/**   
 * @Package com.ebt.Log @Title: CommonLog.java 
 * @author Damon.Han
 * @Description: 日志输出�?
 * @date 2013-12-27 下午3:00:28 
 */
package com.damon.g2048.log;

import com.damon.g2048.ConfigData;

import android.util.Log;

/**
 * @ClassName: CommonLog
 * @author Damon.Han
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @date 2013-12-27 下午3:00:28
 * 
 */
public class ELog {
	private String tag = ConfigData.TAG;
	private boolean isDebug = ConfigData.ISDEBUG;
	public static int logLevel = ConfigData.LOG_LEVEL; // 默认android.util.Log.VERBOSE;
	public ELog() {
	}
	public ELog(String tag) {
		this.tag = tag;
	}

	public void setTag(String tag) {
		this.tag = tag;
	}
	/**
	 * 
	 * @Description: 生成verbose级别的日志
	 * @param
	 * @return void
	 * @throws
	 */
	public void v(Object str) {
		if (isDebug) {
			verbose(str);
		}
	}
	/**
	 * 
	 * @Description: 生成debug级别的日志
	 * @param
	 * @return void
	 * @throws
	 */
	public void d(Object str) {
		if (isDebug) {
			debug(str);
		}
	}
	/**
	 * 
	 * @Description: 生成info级别的日志
	 * @param
	 * @return void
	 * @throws
	 */
	public void i(Object obj) {
		if (isDebug) {
			info(obj);
		}
	}
	/**
	 * 
	 * @Description: 生成warn级别的日志
	 * @param
	 * @return void
	 * @throws
	 */
	public void w(Object str) {
		if (isDebug) {
			warn(str);
		}
	}
	/**
	 * 
	 * @Description: 生成error级别的日志
	 * @param
	 * @return void
	 * @throws
	 */
	public void e(Object str) {
		if (isDebug) {
			error(str);
		}
	}
	/**
	 * 
	 * @Description: 生成error级别的日志
	 * @param ex
	 *            -exception
	 * @return void
	 * @throws
	 */
	public void e(Exception ex) {
		if (isDebug) {
			error(ex);
		}
	}

	private void error(Object str) {
		if (logLevel <= Log.ERROR) {
			String name = getFunctionName();
			String ls = (name == null ? str.toString() : (name + " - " + str));
			Log.e(tag, ls);
		}
	}

	private void debug(Object str) {
		if (logLevel <= Log.DEBUG) {
			String name = getFunctionName();
			String ls = (name == null ? str.toString() : (name + " - " + str));
			Log.d(tag, ls);
		}
	}
	private void error(Exception ex) {
		if (logLevel <= Log.ERROR) {
			StringBuffer sb = new StringBuffer();
			String name = getFunctionName();
			StackTraceElement[] sts = ex.getStackTrace();

			if (name != null) {
				sb.append(name + " - " + ex + "\r\n");
			} else {
				sb.append(ex + "\r\n");
			}

			if (sts != null && sts.length > 0) {
				for (StackTraceElement st : sts) {
					if (st != null) {
						sb.append("[ " + st.getFileName() + ":"
								+ st.getLineNumber() + " ]\r\n");
					}
				}
			}

			Log.e(tag, sb.toString());
		}
	}
	/**
	 * 
	 * @Description: info 级别
	 * @param
	 * @return void
	 * @throws
	 */
	private void info(Object obj) {
		if (logLevel <= Log.INFO) {
			String name = getFunctionName();
			String msg = name == null ? obj.toString() : (name + "-" + obj
					.toString());
			Log.i(tag, msg);
		}
	}

	private void verbose(Object str) {
		if (logLevel <= Log.VERBOSE) {
			String name = getFunctionName();
			String ls = (name == null ? str.toString() : (name + " - " + str));
			Log.v(tag, ls);
		}
	}

	private void warn(Object str) {
		if (logLevel <= Log.WARN) {
			String name = getFunctionName();
			String ls = (name == null ? str.toString() : (name + " - " + str));
			Log.w(tag, ls);
		}
	}
	private String getFunctionName() {
		// 获得当前堆栈中的信息元素集合
		StackTraceElement[] elements = Thread.currentThread().getStackTrace();
		for (StackTraceElement st : elements) {
			if (st.isNativeMethod()) {
				continue;
			} else if (st.getClassName().equals(Thread.class.getName())) {// 剔除线程�?
				continue;
			} else if (st.getClassName().equals(this.getClass().getName())) {// 剔除当前�?
				continue;
			}
			return "[" + Thread.currentThread().getId() + " : "
					+ st.getFileName() + " : " + st.getLineNumber() + "]";

		}
		return null;
	}
}
